//
//  WayBillGuiJiCell.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/11/18.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "WayBillGuiJiCell.h"
#import "XLPhotoBrowser.h"

@interface WayBillGuiJiCell ()<XLPhotoBrowserDelegate>

@property (nonatomic, strong) UILabel *time1Lab;
@property (nonatomic, strong) UILabel *time2Lab;
@property (nonatomic, strong) UILabel *dotLab;
@property (nonatomic, strong) UILabel *topLine;
@property (nonatomic, strong) UILabel *bottomLine;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *userLab;
@property (nonatomic, strong) UIButton *lookBtn;
@property (nonatomic, strong) UILabel *remarkLab;

@end

@implementation WayBillGuiJiCell

+ (instancetype)WayBillGuiJiCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"WayBillGuiJiCell";
    WayBillGuiJiCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[WayBillGuiJiCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = HexColor(0xffffff);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.time1Lab = [UILabel new];
        self.time1Lab.textColor = HexColor(0x9C9DB3);
        self.time1Lab.font = [UIFont systemFontOfSize:12];
        self.time1Lab.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:self.time1Lab];
        
        self.time2Lab = [UILabel new];
        self.time2Lab.textColor = HexColor(0x9C9DB3);
        self.time2Lab.font = [UIFont systemFontOfSize:12];
        self.time2Lab.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:self.time2Lab];
        
        self.dotLab = [UILabel new];
        self.dotLab.backgroundColor = Color_ThemeColor;
        self.dotLab.layer.masksToBounds = YES;
        [self.contentView addSubview:self.dotLab];
        
        self.titleLab = [UILabel new];
        self.titleLab.textColor = HexColor(0x2B2C44);
        self.titleLab.font = BoldFontOfSize(16);
        [self.contentView addSubview:self.titleLab];
        
        self.userLab = [UILabel new];
        self.userLab.textColor = HexColor(0x585974);
        self.userLab.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.userLab];
        
        self.lookBtn = [UIButton new];
        [self.lookBtn setTitle:@"查看附件" forState:0];
        [self.lookBtn setTitleColor:Color_ThemeColor forState:0];
        self.lookBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [self.lookBtn addTarget:self action:@selector(lookClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.lookBtn];
        
        self.remarkLab = [UILabel new];
        self.remarkLab.textColor = HexColor(0x585974);
        self.remarkLab.font = [UIFont systemFontOfSize:12];
        self.remarkLab.numberOfLines = 0;
        [self.contentView addSubview:self.remarkLab];
        
        self.topLine = [UILabel new];
        self.topLine.backgroundColor = HexColor(0xDBDCE6);
        [self.contentView addSubview:self.topLine];
        
        self.bottomLine = [UILabel new];
        self.bottomLine.backgroundColor = HexColor(0xDBDCE6);
        [self.contentView addSubview:self.bottomLine];
        
        self.height = 50;
    }
    return self;
}

- (void)lookClick {
    if (self.model.img.count) {
        XLPhotoBrowser *browser = [XLPhotoBrowser showPhotoBrowserWithImages:self.model.img currentImageIndex:0];
        browser.browserStyle = XLPhotoBrowserStyleIndexLabel;//微博样式
        [browser setActionSheetWithTitle:nil delegate:self cancelButtonTitle:nil deleteButtonTitle:nil otherButtonTitles:@"保存图片", nil];
    }
}

- (void)photoBrowser:(XLPhotoBrowser *)browser clickActionSheetIndex:(NSInteger)actionSheetindex currentImageIndex:(NSInteger)currentImageIndex {
    [browser saveCurrentShowImage];
}

-(void)setModel:(WayBillGuiJiModel *)model {
    _model = model;
    [self configUI];
}

- (void)configUI {
    self.dotLab.nim_width = 14;
    self.dotLab.nim_height = 14;
    self.dotLab.nim_left = 90;
    self.dotLab.nim_top = 15;
    self.dotLab.layer.cornerRadius = 7;
    
    self.topLine.nim_width = 2;
    self.topLine.nim_height = self.dotLab.nim_top;
    self.topLine.nim_top = 0;
    self.topLine.nim_centerX = self.dotLab.nim_centerX;
    
    self.time1Lab.nim_left = 0;
    self.time1Lab.nim_width = self.dotLab.nim_left - 8;
    self.time1Lab.nim_top = self.dotLab.nim_top - 7;
    self.time1Lab.nim_height = 13;
    
    self.time2Lab.nim_left = 0;
    self.time2Lab.nim_width = self.dotLab.nim_left - 8;
    self.time2Lab.nim_top = self.time1Lab.nim_bottom + 2;
    self.time2Lab.nim_height = 13;
    
    NSArray *timeArr = [self.model.time componentsSeparatedByString:@" "];
    if (timeArr.count) {
        self.time1Lab.text = timeArr.firstObject;
        self.time2Lab.text = timeArr.lastObject;
    }
    
    self.titleLab.nim_left = self.dotLab.nim_right + 8;
    self.titleLab.nim_width = ScreenW - 30 - 15;
    self.titleLab.nim_height = 17;
    self.titleLab.nim_centerY = self.dotLab.nim_centerY;
    self.titleLab.text = [NSString stringWithFormat:@"【%@】%@",self.model.city,self.model.title];
    
//    self.userLab.nim_left = self.titleLab.nim_left;
//    self.userLab.nim_top = self.titleLab.nim_bottom + 10;
//    self.userLab.nim_width = self.titleLab.nim_width;
//    self.userLab.nim_height = 13;
//    self.userLab.text = [NSString stringWithFormat:@"签收人：%@",self.model.signuser];
//    
//    CGSize size = [FactoryMethod sizeWithText:@"查看附件" font:self.lookBtn.titleLabel.font maxSize:CGSizeMake(0, 0)];
//    self.lookBtn.nim_width = size.width;
//    self.lookBtn.nim_height = size.height;
//    self.lookBtn.nim_left = self.userLab.nim_left;
//    self.lookBtn.nim_top = self.userLab.nim_bottom + 5;
//    
//    self.remarkLab.nim_left = self.lookBtn.nim_left;
//    self.remarkLab.nim_top = self.lookBtn.nim_bottom + 5;
//    self.remarkLab.nim_width = ScreenW - 30 - 15 - self.remarkLab.nim_left;
//    self.remarkLab.text = self.model.remarks;
//    [self.remarkLab sizeToFit];
//    self.remarkLab.nim_width = ScreenW - 30 - 15 - self.remarkLab.nim_left;
//    
//    if (self.model.remarks.length) {
//        self.height = self.remarkLab.nim_bottom + 20;
//    }else {
//        self.height = self.lookBtn.nim_bottom + 20;
//    }
    
    if ([self.model.status isEqualToString:@"1"]) {
        //到达、出发
        self.userLab.frame = CGRectZero;
        self.lookBtn.frame = CGRectZero;
        self.remarkLab.frame = CGRectZero;
        self.height = self.dotLab.nim_bottom + 35;
    }else if ([self.model.status isEqualToString:@"2"]) {
        //签收
        self.userLab.nim_left = self.titleLab.nim_left;
        self.userLab.nim_top = self.titleLab.nim_bottom + 10;
        self.userLab.nim_width = self.titleLab.nim_width;
        self.userLab.nim_height = 13;
        self.userLab.text = [NSString stringWithFormat:@"签收人：%@",self.model.signuser];

        CGSize size = [FactoryMethod sizeWithText:@"查看附件" font:self.lookBtn.titleLabel.font maxSize:CGSizeMake(0, 0)];
        self.lookBtn.nim_width = size.width;
        self.lookBtn.nim_height = size.height;
        self.lookBtn.nim_left = self.userLab.nim_left;
        self.lookBtn.nim_top = self.userLab.nim_bottom + 5;

        self.remarkLab.nim_left = self.lookBtn.nim_left;
        self.remarkLab.nim_top = self.lookBtn.nim_bottom + 5;
        self.remarkLab.nim_width = ScreenW - 30 - 15 - self.remarkLab.nim_left;
        self.remarkLab.text = self.model.remarks;
        [self.remarkLab sizeToFit];
        self.remarkLab.nim_width = ScreenW - 30 - 15 - self.remarkLab.nim_left;

        if (self.model.remarks.length) {
            self.height = self.remarkLab.nim_bottom + 20;
        }else {
            self.height = self.lookBtn.nim_bottom + 20;
        }

    }else if ([self.model.status isEqualToString:@"3"]) {
        //自行添加
        self.userLab.frame = CGRectZero;

        CGSize size = [FactoryMethod sizeWithText:@"查看附件" font:self.lookBtn.titleLabel.font maxSize:CGSizeMake(0, 0)];
        self.lookBtn.nim_width = size.width;
        self.lookBtn.nim_height = size.height;
        self.lookBtn.nim_left = self.titleLab.nim_left;
        self.lookBtn.nim_top = self.titleLab.nim_bottom + 10;

        self.remarkLab.nim_left = self.lookBtn.nim_left;
        self.remarkLab.nim_top = self.lookBtn.nim_bottom + 5;
        self.remarkLab.nim_width = ScreenW - 30 - 15 - self.remarkLab.nim_left;
        self.remarkLab.text = self.model.remarks;
        [self.remarkLab sizeToFit];
        self.remarkLab.nim_width = ScreenW - 30 - 15 - self.remarkLab.nim_left;

        if (self.model.remarks.length) {
            self.height = self.remarkLab.nim_bottom + 20;
        }else {
            self.height = self.lookBtn.nim_bottom + 20;
        }
    }
    self.bottomLine.nim_top = self.dotLab.nim_bottom;
    self.bottomLine.nim_width = 2;
    self.bottomLine.nim_height = self.height - self.dotLab.nim_bottom;
    self.bottomLine.nim_centerX = self.dotLab.nim_centerX;
    
    self.topLine.hidden = self.model.isFirst;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
